--[[
  1. 比对2个张图片，是否匹配
  2. 获取某个区域的 黑白图

  --- 目前我用这个获取 游戏页面的图片资源的，不作为 给用户 使用的代码
]]
local RxPngDiff = {}

function RxPngDiff:imgUrl(imgFullUrl, imgMinUrl)
  local ctn = 0
  local image = require('tsimg')
  local newImage,msg1 = image.loadFile(imgFullUrl)
  if not image.is(newImage) then
    RxLog('RxPngDiff imgFullUrl load faild =' .. imgFullUrl)
    return ctn
  end

  local cropMat,msg2 = image.loadFile(imgMinUrl)
  if image.is(cropMat) then
    local x,y,degree = image.cvFindImage(cropMat, newImage)
    if x ~= -2 then
      RxLog("RxPngDiff x = " .. x .. "\ny = " .. y .. "\ndegree = " .. degree)
    else
      RxLog("RxPngDiff 找图失败 : " .. y)
    end
  else
    RxLog('RxPngDiff imgMinUrl load faild =' .. imgMinUrl)
    RxLog(msg2)
  end
end

function RxPngDiff:imgUrlTable(imgUrlRegion, assetIconTable, resultDegree, saveTabIndex)
  local iconIndex = 0
  if not imgUrlRegion then
    RxLog('RxPngDiff:imgUrlTable imgUrlRegion = nil')
    return iconIndex
  end

  local iconLen = assetIconTable.length or 0
  if iconLen == 0 then
    RxLog('RxPngDiff:imgUrlTable assetIconTable.length = 0')
    return iconIndex
  end

  resultDegree = resultDegree or 0
  saveTabIndex = saveTabIndex or -2
  local image = require('tsimg')
  local x1, y1, x2, y2 = imgUrlRegion[1],imgUrlRegion[2],imgUrlRegion[3],imgUrlRegion[4]

  local iu1, uname = UtilImageNamePath('diffCvfindImg')
  snapshot(uname, x1, y1, x2, y2)
  mSleep(50)
  local newImage,msg1 = image.loadFile(iu1)

  function delPng()
    -- 测试阶段，可以 注释掉
    delFile(iu1)
  end

  if not image.is(newImage) then
    delPng()
    return iconIndex
  end

  RxLog('RxPngDiffTable saveTabIndex='..tostring(saveTabIndex))
  local preResult = nil

  local num = 1
  while true do
    ::_restart::
    if num > iconLen then
      break
    end

    local uTab = assetIconTable.icon[num]
    local uTabUrl = uTab.url
    local url = UtilImageEnvPath(uTabUrl)
    local cropMat, msg2 = image.loadFile(url)
    if not image.is(cropMat) then
      num = num + 1
      RxLog('RxPngDiffTable 加载 本地图片失败，是否路径正确\n uTabUrl='.. uTabUrl .. ' url='..url)
      goto _restart
    end
------------------------------------------------------------
-- 普通保留截图、比较
    local uTabTitle = uTab.title
    -- newImage 图片尺寸 >  cropMat
    local x,y,degree = image.cvFindImage(newImage, cropMat)
------------------------------------------------------------
-- 二值化 - 黑白图片保留  、比较
    -- local isSave = 0
    -- 保留 - 神秘的箱子 - 截图 - 测试用
    -- 保留 - 精巧的机关 - 截图 - 测试用
    -- if saveTabIndex == uTab.index then
    --   RxLog('RxPngDiffTable save title='..uTabTitle..' index='..tostring(num))
    --   isSave = 1
    -- end

    -- local RxValue = uTab.value
    -- local large = RxPngDiff:binaryzation(newImage, RxValue, isSave)
    -- local min = RxPngDiff:binaryzation(cropMat, RxValue)
    -- local x,y,degree = image.cvFindImage(large, cropMat)
    -------------------------------------------------------------
    if x ~= -2 then
      if degree >= resultDegree then
        RxLog('RxPngDiffTable 成功 uTabTitle='.. uTabTitle .. ' index=' .. tostring(uTab.index) .. '\ndegree=' .. tostring(degree) ..'\n\n')
        if preResult then
          if preResult.degree < degree then
            preResult = uTab
            preResult.degree = degree
          end
        else
          preResult = uTab
          preResult.degree = degree
        end
      else
        RxLog('RxPngDiffTable 相似度较低 uTabTitle='.. uTabTitle .. '\ndegree=' .. tostring(degree) ..'\n\n')
      end
    else
      RxLog('RxPngDiffTable 失败 失败 失败 uTabTitle='.. uTabTitle .. ' x=' .. x .. ' y='.. y.. '\ndegree=' .. tostring(degree))
    end
    mSleep(10)
    num = num + 1
  end
  delPng()

  RxLog('RxPngDiffTable 图片 iu1=' .. iu1)

  if preResult then
    iconIndex = preResult.index
  end
  return iconIndex
end

function RxPngDiff:SnapshotBinImgTable(imgUrlRegion, RxValue, isSave)
  if not imgUrlRegion then
    RxLog('RxPngDiff:SnapshotBinImgTable imgUrlRegion = nil')
    return false
  end

  local defaultValue = {{0xfffefd, 0xfff5d1}}
  RxValue = RxValue or defaultValue
  isSave = isSave or 0

  local image = require('tsimg')
  local x1, y1, x2, y2 = imgUrlRegion[1],imgUrlRegion[2],imgUrlRegion[3],imgUrlRegion[4]
  mSleep(50)
  local iu1 = UtilImageNamePath('snapshotBinImg')
  snapshot(iu1, x1, y1, x2, y2)
  mSleep(50)
  local newImage,msg1 = image.loadFile(iu1)
  if (image.is(newImage)) then
    local large = RxPngDiff:binaryzation(newImage, RxValue, isSave)
  else
    return false
  end
  return true
end

function RxPngDiff:binaryzation(img, RxValue, isSave)
  --690,272,0xfff5d1
  --676,246,0xfffefd
  local image = require('tsimg')
  local defaultValue = {{0xfffefd, 0xfff5d1}}
  RxValue = RxValue or defaultValue
  isSave = isSave or 0
  local binaMat,msg = image.binaryzation(img, RxValue)
  if image.is(binaMat) then
    if isSave == 1 then
      local iu1 = UtilImageNamePath('img_bina')
      local boo,ms = image.saveToPngFile(binaMat, iu1)
    end
    return binaMat
  end
  RxLog('RxPngDiff:binaryzation 二值化错误' .. msg)
  return img
end

return RxPngDiff